import java.util.Arrays;
import java.util.Scanner;

public class day2 {
    /**
     * 数组中两个字符串的最小距离
     * https://www.nowcoder.com/questionTerminal/2c6a0a8e1d20492f92941400036e0890
     */
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        String str1 = in.next();
        String str2 = in.next();

        int index1 = -1,index2 = -1,ret = 0x3f3f3f3f;
        for(int i = 0;i < n;i++) {
            String str = in.nextLine();
            if(str.equals(str1)) {
                //出现str1的字符串，如果存在index2的下标就进行计算ret长度
                //如果没有就进行赋值，记录下来，或者更新
                if(index2 != -1) {
                    // 这个index2一定是在前面出现的，所以i一定比其大
                    ret = Math.min(ret,i - index2);
                }
                // 赋值或者更新
                index1 = i;
            }else if(str.equals(str2)) {
                // 同上
                if(index1 != -1) {
                    ret =Math.min(ret,i - index1);
                }
                index2 = i;
            }
        }
        System.out.println(ret != 0x3f3f3f3f ? ret : -1);
    }


    /**
     * DP4 最小花费爬楼梯
     * https://www.nowcoder.com/practice/9b969a3ec20149e3b870b256ad40844e?tpId=230&tqId=39751&ru=/exam/oj
     */
    public static void main2(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] cost = new int[n];
        for(int i = 0;i < n;i++) {
            cost[i] = in.nextInt();
        }

        int[] dp = new int[n + 1];
        // 因为可以选择 从0或者1下标进行爬楼梯，那么dp[0]=dp[1] = 0
        for(int i = 2;i <= n;i++) {
            dp[i] = Math.min(dp[i - 1] + cost[i - 1],dp[i - 2] + cost[i - 2]);
        }
        System.out.print(dp[n]);
    }

    /**
     * BC64 牛牛的快递
     * https://www.nowcoder.com/practice/41b42e7b3c3547e3acf8e90c41d98270?tpId=290&tqId=39852&ru=/exam/oj
     */
    public static void main1(String[] args) {
        Scanner in = new Scanner(System.in);
        float a = in.nextFloat();
        char b = in.next().charAt(0);
        int price = 0;
        if(a <= 1) {
            price = 20;
        }else {
            price = 20;
            a -= 1;
            while(a > 0) {
                price += 1;
                a -= 1;
            }
        }
        if(b == 'y') {
            price += 5;
        }
        System.out.println(price);
    }
}
